package com.qiezi.project.dialog;

import android.app.Dialog;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.hjq.base.BaseDialog;
import com.hjq.base.BaseDialogFragment;
import com.qiezi.R;
import com.qiezi.base.BaseRecyclerAdapter;
import com.qiezi.bean.PaypalBean;
import com.qiezi.project.decoration.RecyclerItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2018/12/2
 *    desc   : 消息对话框
 */
public class ManageDialog {

    public static class PaypalAdapter extends BaseRecyclerAdapter<PaypalBean>{

        public class VH extends Holder{

            TextView tvMoney,tvStatus,tvDate;

            public VH(View itemView) {
                super(itemView);
                tvMoney = itemView.findViewById(R.id.tv_money);
                tvStatus = itemView.findViewById(R.id.tv_status);
                tvDate = itemView.findViewById(R.id.tv_date);
            }

            public void setData(PaypalBean data){
                tvMoney.setText("金额："+data.getMoney());
                if (data.getStatus() == 0){
                    tvStatus.setText("提现中");
                    tvStatus.setTextColor(0xff333333);
                }else {
                    tvStatus.setText("已打款");
                    tvStatus.setTextColor(0xffe94454);
                }
                tvDate.setText(data.getCreatetime());
            }
        }

        @Override
        public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_paypal, parent, false);
            return new PaypalAdapter.VH(v);
        }

        @Override
        public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, PaypalBean data) {
            ((VH)viewHolder).setData(data);
        }
    }


    public static class Builder
            extends BaseDialogFragment.Builder<Builder>
            implements View.OnClickListener {

        private OnListener mListener;

        private PaypalAdapter adapter;



        public Builder(FragmentActivity activity) {
            super(activity);
            initialize();
        }

        public Builder(FragmentActivity activity, int themeResId) {
            super(activity, themeResId);
            initialize();
        }

        private EditText etAddress;
        private TextView tvClose,tvLogout,tvModify,tvUser;
        private RecyclerView rcv;

        private void initialize() {
            setContentView(R.layout.dialog_manage);
            setAnimStyle(BaseDialog.AnimStyle.SCALE);
            setGravity(Gravity.CENTER);

            etAddress = findViewById(R.id.et_address);
            tvClose = findViewById(R.id.tv_close);
            tvLogout = findViewById(R.id.tv_logout);
            tvModify = findViewById(R.id.tv_modify);
            tvUser = findViewById(R.id.tv_user);

            rcv = findViewById(R.id.rcv_history);

            rcv.setLayoutManager(new LinearLayoutManager(getContext()));

            adapter = new PaypalAdapter();
            adapter.setEmptyViewDef(getContext(),-1);
            adapter.setEmptyText("加载中...");
            adapter.setEmptyVisibility(View.VISIBLE);
            rcv.setAdapter(adapter);
            rcv.addItemDecoration(new RecyclerItemDecoration(6,1));
            rcv.setItemAnimator(new DefaultItemAnimator());

            tvLogout.setOnClickListener(this);
            tvModify.setOnClickListener(this);
            tvClose.setOnClickListener(this);

            etAddress.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                    Log.d("", "onTextChanged: "+s.toString());
                    if (addr != null){
                        if (!s.toString().equals(addr)){
                            tvModify.setEnabled(true);
                        }
                    }

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }

        public Builder setData(ArrayList<PaypalBean> data){
            System.out.println(getDialog().isShowing());
            if (!getDialog().isShowing() || adapter == null) return this;
            adapter.setDatas(data);
            return this;
        }


        public Builder setEmptyText(CharSequence msg){
            if (!getDialog().isShowing() || adapter == null) return this;
            adapter.setEmptyText(msg);
            return this;
        }

        public Builder setUsername(String username){
            tvUser.setText("账号："+username);
            return this;
        }

        private String addr;

        public Builder setAddr(String addr){
            this.addr = addr;
            etAddress.setText(addr);
            return this;
        }

        @Override
        public void dismiss() {
            super.dismiss();
        }

        public Builder setListener(OnListener l) {
            mListener = l;
            return this;
        }


        /**
         * {@link View.OnClickListener}
         */
        @Override
        public void onClick(View v) {
            if (mListener == null) return;

            if (v == tvClose){
                mListener.cancel(getDialog());
                dismiss();
            }else if (v == tvModify){
                mListener.modify(getDialog(),etAddress.getText().toString());
            }else if (v == tvLogout){
                mListener.logout(getDialog());
            }
        }

    }


    public interface OnListener {

        /**
         * 点击修改提现地址时回调
         */
        void modify(Dialog dialog, String address);

        /**
         * 点击退出登录时回调
         */
        void logout(Dialog dialog);
        /**
         * 点击取消时回调
         */
        void cancel(Dialog dialog);
    }
}